package com.vogue.supplymall.shop.domain.dao.impl;

import com.vogue.supplymall.common.domain.dto.SearchInfo;
import com.vogue.supplymall.shop.domain.dao.ProductDao;
import com.vogue.supplymall.shop.domain.mapper.ProductMapper;
import com.vogue.supplymall.shop.domain.model.Product;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * The type Product dao.
 */
@Repository("productDao")
public class ProductDaoImpl extends BaseDaoImpl<Product, Long> implements ProductDao {

    @Resource
    private ProductMapper productMapper;

    @Override
    public Product findById(Long id) {
        return productMapper.findById(id);
    }

    @Override
    public StringBuilder getSqlById(Long id, Long sysCode) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ")
                .append(" p.id pid, ")
                .append(" p.syscodeid, ")
                .append(" p.shopid, ")
                .append(" p.`name` pname, ")
                /*2018-2-28添加matchid*/
                .append(" s.matchid, ")
                .append(" s.`name` sname, ")
                .append(" s.entname, ")
                .append(" p.detailinfo, ")
                .append(" p.sendlvl, ")
                .append(" IF ( p.sendlvl = 1, 'all',  IF ( p.sendlvl = 2, s.area,  IF ( p.sendlvl = 4, s.province,  IF (p.sendlvl = 8, s.city, 0) ) ) ) area, ")
                .append(" p.hiddenflag, ")
                .append(" pa.inventory, ")
                .append(" pa.weight, ")
                .append(" pav.`name` attr_name, ")
                .append(" pav.typeid, ")
                .append(" '0' isWatch, ")
                .append(" pp1.price public_price, ")
                .append(" pp2.price private_price ");
        sql.append(" FROM ")
                .append(" product p ")
                .append(" INNER JOIN shop s ON (s.id = p.shopid) ")
                .append(" INNER JOIN product_attrplan pa ON (p.id = pa.productid  AND pa.defaultflag = 1 ) ")
                .append(" INNER JOIN product_attrplan_detail pad ON (pad.attrplanid = pa.id) ")
                .append(" INNER JOIN product_attrvalue pav ON (pav.id = pad.attrvalueid) ")
                .append(" INNER JOIN product_price pp1 ON ( ")
                .append(" p.id = pp1.productid ")
                .append(" AND pa.id = pp1.attrplanid ")
                .append(" AND pp1.allflag = 1 ")
                .append(" ) ")
                .append(" LEFT JOIN product_price pp2 ON ( ")
                .append(" p.id = pp2.productid ")
                .append(" AND pa.id = pp2.attrplanid ")
                .append(" AND pp2.allflag = 0 ");
        // 如果未登录不匹配隐藏商品的关系
        if (null != sysCode) {
            sql.append(" AND pp2.oneid = ").append(sysCode)
                    // 2018/2/26 匹配时间区间允许的私有价格
                    .append(" AND pp2.begindate < NOW() ")
                    .append(" AND pp2.enddate > NOW() ");
        }
        sql.append(" ) ");
        sql.append(" WHERE ")
                .append(" s.disabledflag = 0 ")
                .append(" AND p.disabledflag = 0 ")
                .append(" AND p.id = ")
                .append(id);
        // 如果未登录只能查询非隐藏商品
        if (!sysCode.equals(0L)) {
            sql.append(" AND p.hiddenflag = 0 ");
        }
        sql.append(" GROUP BY ")
                .append(" pid ");
        return sql;
    }

    @Override
    public List<Map<String, Object>> findProduct(SearchInfo searchInfo) {
        return productMapper.findProduct(searchInfo);
    }

    @Override
    public Long findProductCount(SearchInfo searchInfo) {
        return productMapper.findProductCountBySql(searchInfo);
    }

    @Override
    public List<Map<String, Object>> findProductUnit(SearchInfo searchInfo) {
        return productMapper.findProductUnit(searchInfo);
    }

    @Override
    public List<Product> findProductBySSM(Long sysCodeId, Long shopId, List<String> matchIds) {

        return productMapper.findProductBySSM(sysCodeId, shopId, matchIds);
    }

    @Override
    public List<Map<String, Object>> getProductIdBySp(Long syscodeid, String produdctIds) {
        List<Map<String, Object>> list = new ArrayList<>();

        String sql = "select distinct p.id from product p"
                + " join product_attrplan pa on pa.syscodeid=p.syscodeid and pa.productid=p.id"
                + " where p.id in(" + produdctIds + ") and p.syscodeid=" + syscodeid
                + " and pa.disabledflag=0";
        list = this.query(sql);
        System.out.println(sql);
        return list;
    }

    @Override
    public int changeProductDisabledFlag(Long sysCodeId, String productIds, boolean disabledFlag) {
        String sql = "";
        if (disabledFlag) {
            sql = "update product set disabledflag=0 where sysCodeId=" + sysCodeId + " and id in (" + productIds + ")";
        } else {
            sql = "update product set disabledflag=1 where sysCodeId=" + sysCodeId + " and id in (" + productIds + ")";
        }

        int cnt = this.update(sql);

        return cnt;
    }

    @Override
    public List<Product> findProductBySS(Long sysCodeId, Long shopId) {

        return productMapper.findProductBySS(sysCodeId, shopId);
    }

    @Override
    public List<Map<String, Object>> findProductDUCBySP(Long sysCodeId, String productIds) {
        String sql = "select count(1) count,p.name,pa.productid "
                + " from product_attrplan pa,product p "
                + " where pa.defaultflag=1 and pa.productid=p.id and pa.syscodeid=p.syscodeid"
                + " and p.syscodeid=" + sysCodeId + " and p.id in (" + productIds + ")"
                + " group by pa.productid";

        List<Map<String, Object>> list = this.query(sql);
        return list;
    }

    @Override
    public void changeNoUnitProductDU(Long sysCodeId, String productId) {
        String sql = "update product_attrplan set defaultflag=1 "
        		+ "where syscodeid="+sysCodeId+" and productid='"+productId+"' limit 1";

        this.update(sql);
    }

    @Override
    public void changeManyUnitProductDU(Long sysCodeId, String productId) {
    	 String sql = "update product_attrplan set defaultflag=0 "
         		+ "where syscodeid="+sysCodeId+" and productid='"+productId+"'";

        this.update(sql);
    }

//	@Override
//	public void saveProductCustomerRelation(String values) {
//		String sql="insert into rel_product_code(id,createdate,modifydate,productid,syscodeid,sellsyscodeid,sellshopid) values "+values;
//		this.update(sql);
//		
//	}

	@Override
	public void delProductCustomerRelation(Long sellsyscodeid) {
		String sql="delete from rel_product_code where sellsyscodeid="+sellsyscodeid;
		this.update(sql);		
	}

	@Override
	public String getProductInfoBySS() {
		String sql="select p.name,p.matchid,pa.name unit_name,pa.matchid unit_matchid"  
				 +" from product p,product_attrvalue pa" 
				 +" where p.id=pa.productid and p.syscodeid=#{syscodeId} and p.shopid=#{shopId} " 
				 +" order by p.matchid";
		
		return sql;
	}

}
